﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using ZyGame;
using ZyGame.Editor;

namespace ZyGame.Editor.Process
{
    internal class CopyInternalConfigProcees : IPreprocessBuildWithReport, IPostprocessBuildWithReport, IFilterBuildAssemblies
    {
        public int callbackOrder => 0;

        public string[] OnFilterAssemblies(BuildOptions buildOptions, string[] assemblies)
        {
            List<PathOptions> internalConfigAttribute = AppDomain.CurrentDomain.GetAttributes<PathOptions>();

            string internalConfigPath = Application.dataPath + "/Resources/";
            foreach (var item in internalConfigAttribute)
            {
                if (item.localtion is PathOptions.Localtion.Internal)
                {
                    string src = Application.dataPath + "/../" + item.filepath;
                    string dest = internalConfigPath + Path.GetFileName(item.filepath);
                    if (File.Exists(src) is false)
                    {
                        Client.Console.WriteError($"Not Find Internal Config :{dest}");
                        continue;
                    }
                    Client.Console.WriteLine($"Copy Internal Config To:{dest}");
                    File.Copy(src, dest, true);
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            return assemblies;
        }

        public void OnPostprocessBuild(BuildReport report)
        {
            string internalConfigPath = Application.dataPath + "/Resources/";
            List<PathOptions> internalConfigAttribute = AppDomain.CurrentDomain.GetAttributes<PathOptions>();
            // foreach (var item in internalConfigAttribute)
            // {
            //     if (item.localtion is PathOptions.Localtion.Internal)
            //     {
            //         string filePath = internalConfigPath + Path.GetFileName(item.filepath);
            //         File.Delete(filePath);
            //         Client.Console.WriteLine($"Delete Internal Config:{filePath}");
            //     }
            // }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        public void OnPreprocessBuild(BuildReport report)
        {

        }
    }
}
